const db = require('../db/index')
const xlsx = require('xlsx')
let filePath = ''
exports.employee=(req,res)=>{
    const page = Number(req.query.currentPage) || 1
    const offset = (page-1) *Number(req.query.pageSize)
    const sql = `SELECT *,count(*) over() as total FROM employee LIMIT ${offset},${req.query.pageSize}`
    db.query(sql,(err,result)=>{
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!result) return res.send({
            status: 1,
            message: '获取员工信息失败!'
        })
        return res.send({
            status:0,
            message:'获取员工信息成功!',
            data:result,
            total:result[0].total
        })
    })

}

exports.searchEmployee=(req,res)=>{
    const name = req.query.name
    const sql = `SELECT * FROM employee WHERE name LIKE '%${name}%'`
    db.query(sql,(err,result)=>{
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!result) return res.send({
            status: 1,
            message: '查询员工信息失败!'
        })
        return res.send({
            status:0,
            message:'查询员工信息成功!',
            data:result
        })
    })
}
exports.deleteEmployee=(req,res)=>{
    const id = req.params.id
    const sql = `DELETE FROM employee WHERE id = ${id}`
    db.query(sql,(err,result)=>{
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!result) return res.send({
            status: 1,
            message: '删除员工信息失败!'
        })
        return res.send({
            status:0,
            message:'删除员工信息成功!',
        })
    })
}
exports.uploadEmployeeAvatar = (req, res) => {
    // res.setHeader('Content-Type','image/jeg')
    // res.setHeader('Content-Type','application/json')
   try {
       if(!req.file){
           return res.send({
               status:1,
               message:'上传失败'
           })
       }
       const allowedTypes = ['image/jpeg']
       if (!allowedTypes.includes(req.file.mimetype)) {
           return res.send({
               status:1,
               message:'格式错误'
           })
       }
        filePath = `http://127.0.0.1:3000/public/upload/${req.file.originalname}`
       // const sql = 'INSERT INTO employee set ?'
       // db.query(sql,[filePath])
       return res.send({
           status:0,
           message:'上传成功',
           data:filePath
       })
   } catch (err){
       return res.send({
           status:1,
           message:err
       })
   }
}
exports.addEmployee=(req,res)=>{
    const data = req.body
    const sql = `INSERT INTO employee set ?`
    db.query(sql,{...data,avatar:filePath},(err,result)=>{
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!result) return res.send({
            status: 1,
            message: '添加员工信息失败!'
        })
        return res.send({
            status:0,
            message:'添加员工信息成功!',
            data:null
        })
    })
}